package response

import (
	"encoding/json"
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/time"
)

type ApiData struct {
	//表字段
	ID                      uint64                     `gorm:"primary_key,column:id" json:"id" form:"id"`
	AppID                   uint64                     `gorm:"column:app_id" json:"app_id" form:"app_id"`
	ApiID                   uint64                     `gorm:"column:api_id" json:"api_id" form:"api_id"`
	Date                    string                     `gorm:"column:date" json:"date" form:"date"`
	AppName                 string                     `gorm:"column:app_name" json:"app_name" form:"app_name"`
	HttpMethod              string                     `gorm:"column:http_method" json:"http_method" form:"http_method"`
	HttpPath                string                     `gorm:"column:http_path" json:"http_path" form:"http_path"`
	ZeusMethod              string                     `gorm:"column:zeus_method" json:"zeus_method" form:"zeus_method"`
	ZeusService             string                     `gorm:"column:zeus_service" json:"zeus_service" form:"zeus_service"`
	Level                   uint64                     `gorm:"column:level" json:"level" form:"level"`
	GroupID                 string                     `gorm:"column:group_id" json:"group_id" form:"group_id"`
	ErrorCount              float64                    `gorm:"column:error_count" json:"error_count" form:"error_count"`
	TotalRequestCount       float64                    `gorm:"column:total_request_count" json:"total_request_count" form:"total_request_count"`
	TP99                    float64                    `gorm:"column:tp99" json:"tp99" form:"tp99"`
	TP95                    float64                    `gorm:"column:tp95" json:"tp95" form:"tp95"`
	QPS                     float64                    `gorm:"column:qps" json:"qps" form:"qps"`
	CreateTime              time.Time                  `gorm:"column:create_time" json:"create_time" form:"create_time"`
	Owner                   string                     `gorm:"column:owner" json:"owner" form:"owner"`
	TP99Max                 float64                    `gorm:"column:tp99_max" json:"tp99_max" form:"tp99_max"`
	TP95Max                 float64                    `gorm:"column:tp95_max" json:"tp95_max" form:"tp95_max"`
	QPSMax                  float64                    `gorm:"column:qps_max" json:"qps_max" form:"qps_max"`
	ClientErrorCount        float64                    `gorm:"column:client_error_count" json:"client_error_count" form:"client_error_count"`
	ClientTotalRequestCount float64                    `gorm:"column:client_total_request_count" json:"client_total_request_count" form:"client_total_request_count"`
	SLA                     float64                    `gorm:"column:sla" json:"sla" form:"sla"`
	Owners                  []*models.ApplicationOwner `gorm:"-" json:"owners,omitempty" form:"owners"`
	Status                  int                        `gorm:"-" json:"status" form:"status"` //1:全部启用，0：全部禁用
	Method                  string                     `gorm:"-" json:"method" form:"method"`
	TemplateType            int                        `gorm:"-" json:"template_type"` //1:http 2:zeus
	Value                   float64                    `gorm:"value"`
	Value2                  string                     `json:"value"`
	DisableGraph            *models.DisableGraph       `gorm:"type:json;column:disable_graph" json:"disable_graph"`
}

type ApplicationOwner struct {
	UserID   string `gorm:"column:user_id" json:"user_id,omitempty" form:"user_id"`
	UserName string `gorm:"column:user_name" json:"user_name,omitempty" form:"username"`
}

func (app ApiData) GetOwners() []*models.ApplicationOwner {
	if app.Owner == "" {
		return nil
	}
	var result []*models.ApplicationOwner
	err := json.Unmarshal([]byte(app.Owner), &result)
	if err != nil {
		return result
	}
	return result
}

//声明一个Hero结构体切片类型
type ApiDataSlice []*ApiData

//切片实现Interface 接口的三个方法
//1.Len() ：返回切片的大小
func (m ApiDataSlice) Len() int {
	return len(m)
}

//2.Less(i, j int) :决定使用什么规则进行排序
func (m ApiDataSlice) Less(i, j int) bool {
	return m[i].SLA > m[j].SLA
}

//3.Swap(i, j int) :Less(i, j int)返回true时进行交换
func (m ApiDataSlice) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}
